class YahtzeeGame

  def initialize()
    @upper_scored = {}
  end

  def play()
     while @upper_scored.length < 6 
        turn = Yahtzee::Turn.new
        keep_dice = []
        while !turn.over?
          turn.roll(keep_dice)
          update_keep_dice(turn.dice,keep_dice) unless turn.yahtzee?
        end
        die = keep_dice[0]
        count = turn.dice.to_hash[die]
        @upper_scored[die] = die * count
        puts "\n"
        puts "YAHTZEE!!!" if turn.yahtzee?
        @upper_scored.sort.each { |die,total| puts "Die #{die} : #{total}" } 
     end 
     puts "Total score #{@upper_scored.values.sum}" 
     return @upper_scored
  end

  def update_keep_dice(dice, keep_dice)
     results = dice.rank_result
     results_order = results.keys.sort.reverse
     results_order.each do |count|
       numbers = results[count] 
       numbers.each do |x|
         if @upper_scored[x].nil?
           keep_dice[0] = x
           return
         end  
       end
     end
  end
  
end